/*
 * COPYRIGHT:	2010 Christopher Smith, licenced under the GNU GPL v2; see COPYING in the top level directory
 * PROJECT:		sdedit
 * FILE:		clswinbase.cpp
 * PURPOSE:		base class for classed windows (as opposed to dialog windows)
 *				
 */

class ClassedWindowBase : public WindowBase
{
protected:
	void Create(DWORD ExStyle, LPCTSTR ClassName, LPCTSTR WindowName, DWORD Style, int x, int y, int Width, int Height, HWND Parent, HMENU Menu, HINSTANCE Instance, LPVOID Param)
	{
		Wnd = NULL;
		HWND LateWnd = NULL;

		inc_ref(); //the reference from the underlying window

		PrepareForCreate();

		LateWnd = RaiseOnWin32ErrorT(CreateWindowEx(ExStyle, ClassName, WindowName, Style, x, y, Width, Height, Parent, Menu, Instance, Param));

		if(!Wnd)
		{
			Wnd = LateWnd;
			SetWindowLongPtr(Wnd, GWLP_USERDATA, (ULONG_PTR)this);
			Init();
		}
	}

	virtual LRESULT RecieveMessage(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam) override
	{
		return DefWindowProc(wnd, msg, wparam, lparam);
	}

	static LRESULT ClassedWndProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
	{
		WindowBase *wob;
		
		if(WindowBase::GetWindowObject(wob, wnd, msg, wparam, lparam))
		{
			ClassedWindowBase *wo = static_cast<ClassedWindowBase *>(wob);

			LRESULT Result;

			wo->inc_ref();

			__try
			{
				if(msg == WM_PRE_PROCESS)
					Result = wo->QueryPreProcess(wparam, lparam);
				else
					Result = wo->RecieveMessage(wnd, msg, wparam, lparam);
			}
			__except(ShowErrorMessageBox(exception_info(), wnd))
			{
				Result = DefWindowProc(wnd, msg, wparam, lparam);
			}

			wo->dec_ref();
			return Result;
		}
		else
			return DefWindowProc(wnd, msg, wparam, lparam);
	}
};